package com.liu.nb.controller;


import com.liu.nb.entity.UserRole;
import com.liu.nb.service.IUserRoleService;
import com.liu.nb.util.Result;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 用户和角色中间表 前端控制器
 * </p>
 *
 * @author zk
 * @since 2021-09-02
 */
@RestController
@RequestMapping("/nb/user-role")
public class UserRoleController {
    @Resource
    private IUserRoleService userRoleService;
    @PostMapping("/add/{userId}/{rolesId}")
    //添加用户和角色，一个用户可以对应多个角色
    public Result add(@PathVariable Integer userId,@PathVariable Integer[] rolesId){
        int i=0;
        do{
            UserRole userRole=new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(rolesId[i]);
            if(!userRoleService.save(userRole)){
                return Result.success("no");
            }
            i++;
        }while (i<rolesId.length);
        return Result.success("ok");
    }

}
